const request = require('./request.js')
const MS = require('./ms')
const {
    isString,
    isNumber,
    isUndefined,
    isBoolean,
    isObject,
    isArray
} = require('./dataType')

const tagLibrary = class tagLibrary {

    constructor() {
    }

    findTagLibrarysBySubCategoryId(subCategoryId) {
        let params = {
            subCategoryId: subCategoryId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.FIND_TAG_LIBRARYS_BY_SUB_CATEGORY_ID.url,
                MS._config.parameters.SHOP.FIND_TAG_LIBRARYS_BY_SUB_CATEGORY_ID.method, params).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    get(tagId) {
        let params = {
            tagId: tagId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.GET_TAG_LIBRARY.url,
                MS._config.parameters.SHOP.GET_TAG_LIBRARY.method, params).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    save(data) {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.SAVE_TAG_LIBRARY.url,
                MS._config.parameters.SHOP.SAVE_TAG_LIBRARY.method, {}, data).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    query(options) {
        let params = {
            pageNum: 1,
            pageSize: 10
        }
        if (!isUndefined(options.name) && isString(options.name)) {
            params.name = options.name;
        }
        if (!isUndefined(options.status) && isBoolean(options.status)) {
            params.status = options.status;
        }
        if (!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
            params.pageNum = options.pageNum;
        }
        if (!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
            params.pageSize = options.pageSize;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.FIND_TAG_LIBRARY.url,
                MS._config.parameters.SHOP.FIND_TAG_LIBRARY.method, params).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    updateStatus(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.UPDATE_TAG_LIBRARY_STATUS.url,
                MS._config.parameters.SHOP.UPDATE_TAG_LIBRARY_STATUS.method, params).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    remove(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.REMOVE_TAG_LIBRARY.url,
                MS._config.parameters.SHOP.REMOVE_TAG_LIBRARY.method, params).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    searchName(name) {
        let params = {
            name: name
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.SEARCH_TAG_LIBRARY_BY_NAME.url,
                MS._config.parameters.SHOP.SEARCH_TAG_LIBRARY_BY_NAME.method, params).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }
}

module.exports = tagLibrary
